package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chen.entity.*;
import com.chen.mapper.CouponHistoryMapper;
import com.chen.mapper.CouponProductRegionRelationMapper;
import com.chen.mapper.CouponProductRelationMapper;
import com.chen.service.CouponHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.utils.UserThreadLocal;
import com.chen.vo.MyAllCouponVo;
import com.chen.vo.MyCouponHistoryVo;
import com.chen.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券使用、领取历史表 服务实现类
 * </p>
 *
 * @author cws
 * @since 2024-11-26
 */
@Service
public class CouponHistoryServiceImpl extends ServiceImpl<CouponHistoryMapper, CouponHistory> implements CouponHistoryService {


    @Resource
    private CouponHistoryMapper couponHistoryMapper;

    @Resource
    private CouponProductRelationMapper couponProductRelationMapper;

    @Resource
    private CouponProductRegionRelationMapper couponProductRegionRelationMapper;

    @Override
    public Result UserCouponList(Integer status) {
        User user = UserThreadLocal.get();
        List<MyCouponHistoryVo> list = couponHistoryMapper.UserCouponList(user.getId(), status);
        return Result.success(list);
    }

    @Override
    public Result couponListByOrder(List<CartItem> cartItems) {
        List<Long> regionIds = cartItems.stream().map(item -> item.getRegionId()).collect(Collectors.toList());
        List<Long> productIds = cartItems.stream().map(item -> item.getProductId()).collect(Collectors.toList());
        List<MyCouponHistoryVo> myCouponHistoryVoList = new ArrayList<>();
        User user = UserThreadLocal.get();
        List<MyCouponHistoryVo> list = couponHistoryMapper.UserCouponList(user.getId(), 0);
        for (MyCouponHistoryVo myCouponHistoryVo : list) {
            if (myCouponHistoryVo.getUseType() == 0) {
                myCouponHistoryVoList.add(myCouponHistoryVo);
            }else if (myCouponHistoryVo.getUseType() == 1) {
                LambdaQueryWrapper<CouponProductRegionRelation> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(CouponProductRegionRelation::getCouponId, myCouponHistoryVo.getCouponId());
                wrapper.in(CouponProductRegionRelation::getRegionId, regionIds);
                Integer count = couponProductRegionRelationMapper.selectCount(wrapper);
                if (count > 0){
                    myCouponHistoryVoList.add(myCouponHistoryVo);
                }
            }else if (myCouponHistoryVo.getUseType() == 2) {
                LambdaQueryWrapper<CouponProductRelation> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(CouponProductRelation::getCouponId, myCouponHistoryVo.getCouponId());
                wrapper.in(CouponProductRelation::getProductId, productIds);
                Integer count = couponProductRelationMapper.selectCount(wrapper);
                if (count > 0){
                    myCouponHistoryVoList.add(myCouponHistoryVo);
                }
            }
        }
        return Result.success(myCouponHistoryVoList);
    }
}
